﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Web.Business;
using Web.Core;

namespace Web.Modal
{
    public partial class FunctionApplicationModal : System.Web.UI.UserControl
    {
        #region | Event Paging |

        #region | Const |

        public DataAccessClient dal = new DataAccessClient(DATABASE_PROXY.AUTH);

        private p_USER_PROFILE _userPartial;//= Core.SessionPool.GetInstance().GetSession();

        public object result = null;

        public String message = null;

        private bool _exception = false;

        #endregion

        #region | InitAccess |

        public void InitAccess()
        {
            // Init Access
            AccessToolbar.Resource = "Resource";
            AccessToolbar.SaveText = "Save";
            AccessToolbar.CancelText = "Cancel";
            AccessToolbar.AddVisible = false;
            AccessToolbar.EditVisible = false;
            AccessToolbar.CopyVisible = false;
            AccessToolbar.DeleteVisible = false;
            AccessToolbar.SearchVisible = false;
            //
            AccessToolbar.SaveToolTip = "Save";
            AccessToolbar.CancelToolTip = "CancelOrUndo";
            //
            AccessToolbar.SaveClick += OnSaveClick;
            AccessToolbar.CancelClick += OnCancelClick;
            AccessToolbar.CancelAttributes.Add("OnClick", "pcModal.Hide();");
        }

        private void OnCancelClick(object sender, EventArgs e)
        {

        }

        private void OnSaveClick(object sender, EventArgs e)
        {

        }

        #endregion

        private void Binging(
            string applNo = null,
            string funcNo = null)
        {
            ComboBoxBal.BindApplList(
                page: this.Page,
                cbx: cbbApplication,
                companyCode: _userPartial.COMP_CODE,
                value: applNo,
                text: null,
                showAll:true);

            ComboBoxBal.BindFuncApplList(
                page: this.Page,
                cbx: cbbParentFunction,
                companyCode: _userPartial.COMP_CODE,
                appNo: applNo,
                value: funcNo,
                text: null,
                showAll: true);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _userPartial = Core.SessionPool.GetInstance().GetSession(this.Page);
                InitAccess();
            }
            catch (System.Exception ex)
            {
                //
                Core.Modal.Exception(
                   page: this.Page,
                   message: Core.Exception.ModalException(
                                       method: System.Reflection.MethodBase.GetCurrentMethod(),
                                       ex: ex),
                   headerText: hdnHeader.Value);
                //
            }
        }

        public void OnLoad(
            AccessFlag flag, 
            string applNo = null, 
            string funcNo = null,
            String headerText = null, 
            string companyCode = "HP00")
        {
            try
            {
                if (flag == Core.AccessFlag.Add)
                {
                    txFunctionNo.Text =FunctionApplicationBal.GetInstance()
                                    .GetFuncApplGenerator(
                                                        page: this.Page,
                                                        companyCode: companyCode,
                                                        applNo: applNo,
                                                        parentNo: funcNo);
                    // Binding Combobox
                    Binging(
                        applNo: applNo,
                        funcNo: funcNo);

                    hdnFlag.Value = flag.ToString();

                    hdnHeader.Value = headerText;

                    txFunctionNo.Attributes.Add("readonly", "readonly");

                    Page.ClientScript.RegisterStartupScript(
                                                    type: typeof(bool),
                                                    key: "11DD4D4D-FACC-4A84-BE22-6FDFBB4D86F8",
                                                    script: "pcModal.Show();",
                                                    addScriptTags: true);
                }

                else if (flag == AccessFlag.Copy || flag == AccessFlag.Edit)
                {
                    DataAccess.Prop.FunctionApplication o = FunctionApplicationBal.GetInstance()
                                                                            .Select(
                                                                                page: this.Page,
                                                                                companyCode: _userPartial.COMP_CODE,
                                                                                applNo: applNo,
                                                                                applName: null,
                                                                                funcNo: funcNo,
                                                                                funcName: null,
                                                                                optionMobile: null).First();
                    if(o != null){

                        if (flag == AccessFlag.Copy)
                            txFunctionNo.Text = FunctionApplicationBal.GetInstance()
                                                                    .GetFuncApplGenerator(
                                                                                    page: this.Page,
                                                                                    companyCode: companyCode,
                                                                                    applNo: applNo,
                                                                                    parentNo: funcNo);

                        else txFunctionNo.Text = o.FUNCNO;

                        txFunctionName.Text = o.FUNCNAME;
                        txMainScreenPath.Text = o.MAINSCREEN;
                        chkCentre.Checked = (o.PDAFLAG == 'Y' ) ? true : false;
                        txRemark.Text = o.REMARK;
                    }
                    // Binding Combobox
                    Binging(
                        applNo: applNo,
                        funcNo: funcNo);

                    hdnFlag.Value = flag.ToString();

                    hdnHeader.Value = headerText;

                    txFunctionNo.Attributes.Add("readonly", "readonly");

                    Page.ClientScript.RegisterStartupScript(
                                                        type: typeof(bool),
                                                        key: "11DD4D4D-FACC-4A84-BE22-6FDFBB4D86F8",
                                                        script: "pcModal.Show();",
                                                        addScriptTags: true);
                
                }
                else
                {
                    Core.Modal.Toggle(
                                page: this.Page,
                                modal: ModalFlag.Hide,
                                flag: MessageFlag.None,
                                massage: null,
                                headerText: hdnHeader.Value);
                }

            }
            catch (System.Exception ex)
            {
                //
                Core.Modal.Exception(
                   page: this.Page,
                   message: Core.Exception.ModalException(
                                       method: System.Reflection.MethodBase.GetCurrentMethod(),
                                       ex: ex),
                   headerText: hdnHeader.Value);
                //
            }
        }

        #endregion
    }
}